/* eslint-disable default-case */
import { message } from 'antd';
import { signSuccess, confirmInter } from '@/services/addempsocial';
import * as service from '@/services/socket';
import { SocketActions, batchTaskFactory } from '@/models/socket';

const namespace = 'addsocialbatch';

export default {
  namespace,
  state: {
    resultInfo: {
      // 结果数据必传
      batchName: '', // 任务名称
      taskCount: 0, // 导入数据
      successCount: 0, // 成功数据
      errorCount: 0, // 失败数据
      batchNo: '', // 批次号
      errorUrl: '/osApi/insAddDetExt/syncInsAddDeclDeclErrMessage', // 下载错误信息地址
      errorParams: null, // 下载错误数据参数
    },
    showProgress: false,
    progressPercent: 0,
  },
  effects: {
    // 上传文件:批量进度
    *uploadFileWithProgress(
      {
        payload: { params, type },
      },
      { call, put }
    ) {
      // type === '1' 是标记申报成功 '2'为取消申报
      const response =
        type === '1' ? yield call(signSuccess, params) : yield call(confirmInter, params);
      yield put({
        type: 'updateModelData',
        payload: {
          progressPercent: 0,
        },
      });
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            batchId: response.data && response.data.batchId,
            progressPercent: 0,
            showProgress: true,
          },
        });
        if (response.data && response.data.successProp !== 100) {
          const { batchId } = response.data;
          const batchTask = batchTaskFactory(batchId, namespace, 'handleBatchTask');

          yield put.resolve({
            type: `socket/${SocketActions.subSocketChannel}`,
            payload: {
              batchTask,
            },
          });
        } else {
          yield put({
            type: 'updateBatchTask',
            payload: response.data,
          });
        }
      } else {
        // 上传失败
        message.warning(`${response.message || '服务器端错误'}`);
      }
      return response;
    },
    *matchBatchId({ payload }, { put, select }) {
      const { batchId } = yield select(state => state.addsocialbatch);
      if (batchId === payload.batchId) {
        yield put({
          type: 'handleBatchTask',
          payload,
        });
      }
    },
    *handleBatchTask({ payload }, { put, select }) {
      const state = yield select(s => s.addsocialbatch);
      let { resultInfo } = state;
      if (payload.successProp >= 100) {
        resultInfo.errorParams = {
          batNo: payload.batchId,
        };
        resultInfo = { ...resultInfo, ...payload };
        // 先更新进度
        yield put({
          type: 'updateBatchTask',
          payload: {
            progressPercent: payload.successProp,
            resultInfo,
          },
        });
      } else {
        yield put({
          type: 'updateBatchTask',
          payload: { progressPercent: payload.successProp, resultInfo },
        });
      }
    },
  },

  reducers: {
    // 更新数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 更新批量任务数据
    updateBatchTask(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  subscriptions: {
    socket({ dispatch }) {
      service.listen(async action => {
        // 判断是否自己命名空间的回调

        if (action.namespace === namespace) {
          switch (action.type) {
            case 'task':
              await dispatch({
                type: 'matchBatchId',
                payload: action.data,
              });
              await dispatch({
                type: `socket/${SocketActions.handleUpdateBatchTask}`,
                payload: {
                  successProp: action.data.successProp,
                  batchId: action.data.batchId,
                },
              });
              // 全局保存
              dispatch({
                type: 'publicdata/updateBatchTaskData',
                payload: action.data,
              });
              break;
          }
        }
      }, namespace);
    },
  },
};
